package com.covid.validation;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class SymptomValidator {
    private static final Set<String> VALID_SYMPTOMS = Collections.unmodifiableSet(
            new HashSet<>(Arrays.asList(
                    "发热", "咳嗽", "乏力", "呼吸困难", "都没有"
            ))
    );

    public static void validate(Set<String> symptoms, Double temperature) {
        if (symptoms == null || symptoms.isEmpty()) {
            throw new IllegalArgumentException("至少需要选择一个症状");
        }

        // 验证症状合法性
        if (!VALID_SYMPTOMS.containsAll(symptoms)) {
            throw new IllegalArgumentException("包含无效症状选项");
        }

        // 处理"都没有"的特殊情况
        if (symptoms.contains("都没有")) {
            if (symptoms.size() > 1) {
                throw new IllegalArgumentException("选择'都没有'时不能选择其他症状");
            }
            if (temperature != null) {
                throw new IllegalArgumentException("无症状时不能填写体温");
            }
            return;
        }

        // 体温关联验证
        boolean hasFever = symptoms.contains("发热");
        if (hasFever) {
            if (temperature == null) {
                throw new IllegalArgumentException("选择发热时必须填写体温");
            }
            if (temperature < 34.0 || temperature > 42.0) {
                throw new IllegalArgumentException("体温必须在34℃-42℃之间");
            }
        } else if (temperature != null) {
            throw new IllegalArgumentException("未选择发热时不能填写体温");
        }
    }
}